package com.dinree.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;

import com.base.library.BaseActivity;
import com.base.library.adapter.HomeFragmentPagerAdapter;
import com.dinree.R;
import com.dinree.bean.UserBean;
import com.dinree.databinding.ActivityMainBinding;
import com.dinree.fragment.ClassFragment;
import com.dinree.fragment.HomeFragment;
import com.dinree.fragment.MineFragment;
import com.dinree.fragment.ShoppingCartFragment;
import com.tencent.smtt.sdk.CookieManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private ActivityMainBinding activityMainBinding;
    private List<Fragment> homeFragmentList;
    private HomeFragmentPagerAdapter pagerAdapter;
    private UserBean userBean;
    private static MainActivity mainActivity;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    public static MainActivity getInstance() {
        return mainActivity;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        activityMainBinding = getViewDataBinding();
        mainActivity = this;
        userBean = realm.findFirst(UserBean.class);
        if (userBean != null) {
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setCookie("user_info", "think:{\"user_id\":\"" + userBean.user_id + "\",\"user_token\":\"" + userBean.user_token + "\"}");
        }
        setBarColor(R.color.black3);
        initFragment();
        pagerAdapter = new HomeFragmentPagerAdapter(getSupportFragmentManager(), homeFragmentList);
        activityMainBinding.homeViewPager.setAdapter(pagerAdapter);
        activityMainBinding.homeViewPager.setOffscreenPageLimit(4);
        activityMainBinding.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.home) {
                    //                   setBarColor(R.color.bar_white);
                    activityMainBinding.homeViewPager.setCurrentItem(0, false);
                    HomeFragment homeFragment = (HomeFragment) homeFragmentList.get(0);
                    homeFragment.updatePage();
                }
                if (checkedId == R.id.clazz) {
                    //                     setBarColor(R.color.bar_white);
                    activityMainBinding.homeViewPager.setCurrentItem(1, false);
                    ClassFragment classFragment = (ClassFragment) homeFragmentList.get(1);
                    classFragment.updatePage();
                }
                if (checkedId == R.id.shop_car) {
//                    setBarColor(R.color.mainColor);
                    activityMainBinding.homeViewPager.setCurrentItem(2, false);
                    ShoppingCartFragment shoppingCartFragment = (ShoppingCartFragment) homeFragmentList.get(2);
                    shoppingCartFragment.updatePage();
                }
                if (checkedId == R.id.mine) {
//                    setBarColor(R.color.mainColor);
                    UserBean user = realm.findFirst(UserBean.class);
                    if (user != null) {
                        activityMainBinding.homeViewPager.setCurrentItem(3, false);
                    } else {
                        Intent intent = new Intent(mContext, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        MineFragment fragment = (MineFragment) homeFragmentList.get(3);
        if (fragment != null) {
            fragment.loadData();
        }
    }

    private void initFragment() {
        homeFragmentList = new ArrayList<Fragment>();
        homeFragmentList.add(new HomeFragment());
        homeFragmentList.add(new ClassFragment());
        homeFragmentList.add(new ShoppingCartFragment());
        homeFragmentList.add(new MineFragment());
    }

    private void setBarColor(int id) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(getResources().getColor(id));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
